package com.furnitur.modules.admin.controller.system;

import com.furnitur.common.api.CommonPage;
import com.furnitur.common.api.CommonResult;
import com.furnitur.core.entity.user.User;
import com.furnitur.modules.admin.dto.auth.PasswordUpdateDTO;
import com.furnitur.modules.admin.dto.user.StatusUpdateDTO;
import com.furnitur.modules.admin.dto.user.UserQueryDTO;
import com.furnitur.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/info")
    public CommonResult<User> getUserInfo(@AuthenticationPrincipal User user) {
        return CommonResult.success(user);
    }

    /**
     * 查询用户列表
     * @param queryDTO 查询参数
     * @return 分页用户列表
     */
    @GetMapping("/list")
    public CommonResult<CommonPage<User>> list(@Valid UserQueryDTO queryDTO) {
        if (queryDTO.getPageNum() == null || queryDTO.getPageNum() < 1) {
            queryDTO.setPageNum(1);
        }
        if (queryDTO.getPageSize() == null || queryDTO.getPageSize() < 1) {
            queryDTO.setPageSize(10);
        }
        return CommonResult.success(userService.listUsers(queryDTO));
    }

    @PutMapping("/password")
    public CommonResult<String> updatePassword(@AuthenticationPrincipal User user,
                                             @Valid @RequestBody PasswordUpdateDTO passwordUpdate) {
        userService.updatePassword(user.getId(), passwordUpdate);
        return CommonResult.success("密码修改成功");
    }

    /**
     * 更新用户状态
     * @param id 用户ID
     * @param statusUpdate 状态更新请求
     * @return 更新结果
     */
    @PutMapping("/{id}/status")
    public CommonResult<String> updateStatus(@PathVariable Long id, 
            @Valid @RequestBody StatusUpdateDTO statusUpdate) {
        if (statusUpdate.getStatus() != 0 && statusUpdate.getStatus() != 1) {
            return CommonResult.failed("状态值只能为0或1");
        }
        userService.updateUserStatus(id, statusUpdate.getStatus());
        return CommonResult.success("状态更新成功");
    }

    @PostMapping("/avatar")
    public CommonResult<String> uploadAvatar(@AuthenticationPrincipal User user,
                                           @RequestParam("file") MultipartFile file) {
        try {
            if (user == null) {
                return CommonResult.unauthorized("请先登录");
            }
            String avatarUrl = userService.uploadAvatar(user, file);
            return CommonResult.success(avatarUrl);
        } catch (Exception e) {
            return CommonResult.failed("上传头像失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public CommonResult<Void> deleteUser(@PathVariable Long id) {
        try {
            userService.deleteUser(id);
            return CommonResult.success(null);
        } catch (Exception e) {
            return CommonResult.failed("删除用户失败: " + e.getMessage());
        }
    }
}